﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FBA.Client.Utils
{
    public partial class DateUtils
    {

        #region 获取 本周、本月、本季度、本年 的开始时间或结束时间
        /// <summary>
        /// 获取结束时间
        /// </summary>
        /// <param name="TimeType">Week、Month、Season、Year</param>
        /// <param name="now"></param>
        /// <returns></returns>
        public static DateTime GetTimeStartByType(string TimeType, DateTime now)
        {
            switch (TimeType)
            {
                case "Week":
                    return now.AddDays(-(int)now.DayOfWeek + 1);
                case "Month":
                    return now.AddDays(-now.Day + 1);
                case "Season":
                    var time = now.AddMonths(0 - ((now.Month - 1) % 3));
                    return time.AddDays(-time.Day + 1);
                case "Year":
                    return now.AddDays(-now.DayOfYear + 1);
                default:
                    throw new Exception("Invalid TimeType");
            }
        }

        /// <summary>
        /// 获取结束时间
        /// </summary>
        /// <param name="TimeType">Week、Month、Season、Year</param>
        /// <param name="now"></param>
        /// <returns></returns>
        public static DateTime GetTimeEndByType(string timeType, DateTime now)
        {
            switch (timeType)
            {
                case "Week":
                    return now.AddDays(7 - (int)now.DayOfWeek);
                case "Month":
                    return now.AddMonths(1).AddDays(-now.AddMonths(1).Day + 1).AddDays(-1);
                case "Season":
                    var time = now.AddMonths((3 - ((now.Month - 1) % 3) - 1));
                    return time.AddMonths(1).AddDays(-time.AddMonths(1).Day + 1).AddDays(-1);
                case "Year":
                    var time2 = now.AddYears(1);
                    return time2.AddDays(-time2.DayOfYear);
                default:
                    throw new Exception("Invalid TimeType");
            }
        }

        public static DateTime? GetTime(string timeType)
        {
            DateTime dt = DateTime.Now;

            if(timeType == null)
            {
                return null;
            }
            //今天
            else if (timeType.Equals("Today"))
            {
            }
            //昨天
            else if (timeType.Equals("Yesterday"))
            {
                dt.AddDays(-1);
            }
            //明天
            else if (timeType.Equals("Tomorrow"))
            {
                dt.AddDays(+1);
            }
            //上周今天
            else if (timeType.Equals("LastWeekToday"))
            {
                dt.AddDays(-7);
            }
            //下周今天
            else if (timeType.Equals("NextWeekToday"))
            {
                dt.AddDays(+7);
            }
            //上月今天
            else if (timeType.Equals("LastMonthToday"))
            {
                dt.AddMonths(-1);
            }
            //下月今天
            else if (timeType.Equals("NextMonthToday"))
            {
                dt.AddMonths(+1);
            }
            //上周昨天
            else if (timeType.Equals("LastWeekYesterday"))
            {
                dt.AddDays(-1).AddDays(-7);
            }
            //下周昨天
            else if (timeType.Equals("NextWeekYesterday"))
            {
                dt.AddDays(-1).AddDays(+7);
            }
            //上月昨天
            else if (timeType.Equals("LastMonthYesterday"))
            {
                dt.AddDays(-1).AddMonths(-1);
            }
            //下月昨天
            else if (timeType.Equals("NextMonthYesterday"))
            {
                dt.AddDays(-1).AddMonths(+1);
            }
            //本月第一天
            else if (timeType.Equals("MonthFirst"))
            {
                dt = GetTimeStartByType("Month", dt);
            }
            //上月第一天
            else if (timeType.Equals("LastMonthFirst"))
            {
                dt = GetTimeStartByType("Month", dt.AddMonths(-1));
            }
            //下月第一天
            else if (timeType.Equals("NextMonthFirst"))
            {
                dt = GetTimeStartByType("Month", dt.AddMonths(+1));
            }
            //本月最后一天
            else if (timeType.Equals("MonthEnd"))
            {
                dt = GetTimeEndByType("Month", dt);
            }
            //上月最后一天
            else if (timeType.Equals("LastMonthEnd"))
            {
                dt = GetTimeEndByType("Month", dt.AddMonths(-1));
            }
            //下月最后一天
            else if (timeType.Equals("NextMonthEnd"))
            {
                dt = GetTimeEndByType("Month", dt.AddMonths(+1));
            }
            //本周一
            else if (timeType.Equals("WeekFirst"))
            {
                dt = GetTimeStartByType("Week", dt);
            }
            //上周一
            else if (timeType.Equals("LastWeekFirst"))
            {
                dt = GetTimeStartByType("Week", dt.AddDays(-7));
            }
            //下周一
            else if (timeType.Equals("NextWeekFirst"))
            {
                dt = GetTimeStartByType("Week", dt.AddDays(+7));
            }
            //本周日
            else if (timeType.Equals("WeekEnd"))
            {
                dt = GetTimeEndByType("Week", dt);
            }
            //上周日
            else if (timeType.Equals("LastWeekEnd"))
            {
                dt = GetTimeEndByType("Week", dt.AddDays(-7));
            }
            //下周日
            else if (timeType.Equals("NextWeekEnd"))
            {
                dt = GetTimeEndByType("Week", dt.AddDays(+7));
            }
            //本季度第一天
            else if (timeType.Equals("SeasonFirst"))
            {
                dt = GetTimeStartByType("Season", dt);
            }
            //上季度第一天
            else if (timeType.Equals("LastSeasonFirst"))
            {
                dt = GetTimeStartByType("Season", dt.AddMonths(-3));
            }
            //下季度第一天
            else if (timeType.Equals("NextSeasonFirst"))
            {
                dt = GetTimeStartByType("Season", dt.AddMonths(+1));
            }
            //本季度最后一天
            else if (timeType.Equals("SeasonEnd"))
            {
                dt = GetTimeEndByType("Season", dt);
            }
            //上季度最后一天
            else if (timeType.Equals("LastSeasonEnd"))
            {
                dt = GetTimeEndByType("Season", dt.AddMonths(-3));
            }
            //下季度最后一天
            else if (timeType.Equals("NextSeasonEnd"))
            {
                dt = GetTimeEndByType("Season", dt.AddMonths(+3));
            }
            //今年第一天
            else if (timeType.Equals("YearFirst"))
            {
                dt = GetTimeStartByType("Year", dt);
            }
            //去年第一天
            else if (timeType.Equals("LastYearFirst"))
            {
                dt = GetTimeStartByType("Year", dt.AddYears(-1));
            }
            //明年第一天
            else if (timeType.Equals("NextYearFirst"))
            {
                dt = GetTimeStartByType("Year", dt.AddYears(+1));
            }
            //今年最后一天
            else if (timeType.Equals("YearEnd"))
            {
                dt = GetTimeEndByType("Year", dt);
            }
            //去年最后一天
            else if (timeType.Equals("LastYearEnd"))
            {
                dt = GetTimeEndByType("Year", dt.AddYears(-1));
            }
            //明年最后一天
            else if (timeType.Equals("NextYearEnd"))
            {
                dt = GetTimeEndByType("Year", dt.AddYears(+1));
            }
            //如果timeType不符合，直接返回null
            else
            {
                return null;
            }
            return dt;
        }

            #endregion
        }
}
